<script lang="ts" setup>
import { reactive } from 'vue'

const formInline = reactive({
    region1: '',
    region2: '',
})

const onSubmit = () => {
    console.log('submit!')
}
import { ref } from 'vue'

const currentPage = ref(1)
const pageSize = ref(4)
const dynasty = ref('')
const name = ref('')
const totalpage = ref()
const small = ref(false)
const background = ref(false)
const disabled = ref(false)
const dialogVisible = ref(false)
const dialogname = ref()
const displaydel = ref(false)

const handleSizeChange = (size) => {
    pageSize.value = size
    authorData()
}
const handleCurrentChange = (current) => {
    currentPage.value = current
    authorData()
}
import {
    Delete,
    Edit,
    WarningFilled
} from '@element-plus/icons-vue'

import { authorlistServer } from '@/api/author.js';
import { ElMessage, FormRules } from 'element-plus';

const tableData = ref()

const reg = /^[\u4e00-\u9fa5]+$/
interface RuleForm {
    name:String,
    bi:String,
    id:Number,
    dynasty:String
}

const addData = ref<RuleForm>({
    name: '',
    bi: '',
    dynasty: '',
    id:null,
})


const authorData = async () => {
    let params = {
        countpage: currentPage.value,
        pagenumber: pageSize.value,
        dynasty: dynasty.value ? dynasty.value : null,
        name: name.value ? name.value : null
    }

    let result = await authorlistServer(params)

    tableData.value = result.data.items
    totalpage.value = result.data.total
}

authorData();

const clearadddata = () => {
    addData.value = {
        name:'',
        bi:'',
        dynasty:'',
        id:null
    }
}

import { addauthorServer,updataauthorServer,delauthorServer } from '@/api/author.js';
const addauthor = async () => {
    dialogVisible.value = false;
    let result = await addauthorServer(addData.value) as any;
    ElMessage.success(result.message ? result.message : '添加成功')
    clearadddata()
    authorData()
}

const updataauthor = async ()=>{
    dialogVisible.value = false;
    let result = await updataauthorServer(addData.value) as any;
    ElMessage.success(result.message ? result.message : '修改成功')
    clearadddata()
    authorData()
}

const delauthor = async ()=>{
    displaydel.value = false
    let result = await delauthorServer(addData.value.id) as any;
    ElMessage.success(result.message ? result.message : '删除成功')
    clearadddata()
    authorData()
}

const rules = reactive<FormRules<RuleForm>>({
   name:[
      {required:true,message:'请输入作者姓名',trigger:'blur'},
      {pattern:reg,message:'姓名应是全中文',trigger:'blur'}
   ],


   bi:[
      {required:true,message:'请输入作者背景',trigger:'blur'},
      {pattern:reg,message:'背景应是全中文',trigger:'blur'}
   ]
})

const showupdataDiglog = (row)=>{
    dialogVisible.value = true
    dialogname.value = '修改作者'
    addData.value.name = row.name
    addData.value.bi = row.bi
    addData.value.dynasty = row.dynasty
    addData.value.id = row.id

}

const showdelDiglog = (row)=>{
    displaydel.value = true
    dialogname.value ='提示'
    addData.value.id = row.id

}



</script>


<template>

    <el-card style="max-width: 100%">
        <div class="card-header">
            <span>作者管理</span>
            <el-button type="primary" @click="dialogVisible = true,dialogname = '添加作者'">添加作者</el-button>
        </div>
        <div style="margin-top: 20px;">
            <hr>
        </div>
        <el-form :inline="true" class="form-inline">
            <el-form-item label = "姓名">
                <el-input placeholder="请输入作者姓名" v-model="name"></el-input>
            </el-form-item>
            <el-form-item label="朝代">
                <el-select v-model="dynasty" placeholder="请选择" clearable>
                    <el-option label="宋朝" value="宋朝" />
                    <el-option label="唐朝" value="唐朝" />
                    <el-option label="三国" value="三国" />
                    <el-option label="战国" value="战国" />
                    <el-option label="西汉" value="西汉" />
                    <el-option label="东汉" value="东汉" />
                    <el-option label="清朝" value="清朝" />
                    <el-option label="春秋" value="春秋" />
                    <el-option label="现代" value="现代" />
                </el-select>
            </el-form-item>
            <el-form-item>
                <el-button type="primary" @click="authorData()">搜索</el-button>
            </el-form-item>
            <el-form-item>
                <el-button type="default" @click="dynasty = '',name='',authorData()">重置</el-button>
            </el-form-item>
        </el-form>
        <el-table :data="tableData" style="width: 100%" table-layout='auto'>
            <el-table-column prop="id" label="id" />
            <el-table-column prop="name" label="姓名" />
            <el-table-column prop="dynasty" label="朝代" />
            <el-table-column prop="bi" label="背景" />
            <el-table-column label="操作" width="180">
                <template #default="{row}">
                <el-button type="primary" :icon="Edit" circle @click="showupdataDiglog(row)"/>
                <el-button type="danger" :icon="Delete" circle @click="showdelDiglog(row)"/>
                </template>
            </el-table-column>
        </el-table>

        <el-dialog v-model="dialogVisible" :title="dialogname" width="500">
            <el-form ref="form" class="addauthor" :rules="rules" :model="addData">
                <el-form-item label="姓名" prop="name">
                    <el-input placeholder="请输入姓名" v-model="addData.name"></el-input>
                </el-form-item>
                <el-form-item label="背景" prop="bi">
                    <el-input placeholder="请输入背景" v-model="addData.bi"></el-input>
                </el-form-item>
                <el-form-item label="朝代">
                    <el-select v-model="addData.dynasty" placeholder="请选择" clearable>
                        <el-option label="宋朝" value="宋朝" />
                        <el-option label="唐朝" value="唐朝" />
                        <el-option label="三国" value="三国" />
                        <el-option label="战国" value="战国" />
                        <el-option label="西汉" value="西汉" />
                        <el-option label="东汉" value="东汉" />
                        <el-option label="清朝" value="清朝" />
                        <el-option label="春秋" value="春秋" />
                        <el-option label="现代" value="现代" />
                    </el-select>
                </el-form-item>
            </el-form>
            <template #footer>
                <div class="dialog-footer">
                    <el-button @click="dialogVisible = false, clearadddata()">取消</el-button>
                    <el-button type="primary" @click="dialogname=='添加作者'? addauthor():updataauthor()">确认</el-button>
                </div>
            </template>
        </el-dialog>

        <el-dialog v-model="displaydel" :title=dialogname width="500" >
            <el-icon color="red"><WarningFilled /></el-icon>
            <span style="text-align: center;display:block;font-size: 17px;">是否确认删除该作者</span>
            <template #footer>
                <div class="dialog-footer">
                    <el-button @click="displaydel = false, clearadddata()">否</el-button>
                    <el-button type="primary" @click="delauthor()">是</el-button>
                </div>
            </template>
        </el-dialog>

    

        <!--layout控制组件展示顺序-->
        <el-pagination class="el-p" v-model:current-page="currentPage" v-model:page-size="pageSize"
            :page-sizes="[4, 8, 12, 16]" :small="small" :disabled="disabled" :background="background"
            layout="jumper, prev, pager, next, sizes, total" :total="totalpage" @size-change="handleSizeChange"
            @current-change="handleCurrentChange" />
    </el-card>
</template>

<style scoped>
.el-p {
    margin-top: 20px;
    display: flex;
    justify-content: flex-end;
}

.form-inline .el-input {
    --el-input-width: 220px;
}

.form-inline .el-select {
    --el-select-width: 220px;
}

.card-header {
    display: flex;
    justify-content: space-between;
    margin-inline-end: 20px;
}
</style>